package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.Equipment;
import com.woniuxy.factory.entity.RepairApply;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.service.EquipmentService;
import com.woniuxy.factory.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/repair")
public class RepairEquipmentController {
    @Autowired
    RepairService repairService;
    @Autowired
    EquipmentService equipmentService;

    @ResponseBody
    @RequestMapping("/accept")
    @Transactional
    //维修人员点击接受申请表单后，将设备状态置为“维修中”，同时维修申请表中的对应项置为“已接受”。
    public ResponseResult repairEquipment(RepairApply repairApply) {
        ResponseResult<Object> res = new ResponseResult<>();
        //参数验证
        if (repairApply.getEid() == null || repairApply.getRepId() == null) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        try {
            //将设备状态置为维修中
            Equipment equipment = new Equipment();
            equipment.setEid(repairApply.getEid());
            equipment.setState("维修中");
            equipmentService.modifyEquipment(equipment);
            //将申请表中的对应项置为已接受
            repairApply.setState("已接受");
            repairService.modifyRepair(repairApply);
        } catch (Exception e) {
            res.setCode(1);
            res.setMsg("异常错误，请重试");
            return res;
        }
        res.setCode(0);
        res.setMsg("接单成功");
        return res;
    }

    @ResponseBody
    @RequestMapping("/finish")
    @Transactional
    //维修人员完成维修任务后，点击完成按钮，将设备状态置为“正常”，同时申请表中对应项置为“完成”。
    public ResponseResult finishRepairEquipment(RepairApply repairApply) {
        ResponseResult<Object> res = new ResponseResult<>();
        //参数验证
        if (repairApply.getEid() == null || repairApply.getRepId() == null) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        try {
            //将设备状态置为维修中
            Equipment equipment = new Equipment();
            equipment.setEid(repairApply.getEid());
            equipment.setState("正常");
            equipmentService.modifyEquipment(equipment);
            //将申请表中的对应项置为已接受
            repairApply.setState("已完成");
            repairService.modifyRepair(repairApply);
            //删除已维修好的申请条目
            //repairService.deletRepair(repairApply);
        } catch (Exception e) {
            res.setCode(1);
            res.setMsg("异常错误，请重试");
            return res;
        }
        res.setCode(0);
        res.setMsg("成功");
        return res;
    }
    @ResponseBody
    @RequestMapping("/scrap")
    @Transactional
    //维修人员完成维修任务后，点击完成按钮，将设备状态置为“正常”，同时申请表中对应项置为“完成”。
    public ResponseResult scrapEquipment(RepairApply repairApply){
        ResponseResult<Object> res = new ResponseResult<>();
        //参数验证
        if (repairApply.getEid() == null || repairApply.getRepId() == null) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        try {
            //将设备状态置为报废
            Equipment equipment = new Equipment();
            equipment.setEid(repairApply.getEid());
            equipment.setState("报废");
            equipmentService.modifyEquipment(equipment);
            //将申请表中的对应项置为已接受
            //repairApply.setState("已完成");
            //repairService.modifyRepair(repairApply);
            //删除申请条目
            repairService.deletRepair(repairApply);
        } catch (Exception e) {
            res.setCode(1);
            res.setMsg("异常错误，请重试");
            return res;
        }
        res.setCode(0);
        res.setMsg("成功");
        return res;
    }
}
